import java.util.Arrays;

public class Solution {
    public static int minCut(String s) {
        int len = s.length();
        if (len == 1) {
            return 0;
        }
        char[] ch = s.toCharArray();

        //0-i之间字符串的最少切割次数
        int[] dp = new int[len];

        for (int i = 0; i < len; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        dp[0] = 0;

        //用来判断i-j之间的字符串是否为回文子串
        boolean[][] arr = new boolean[len][len];

        for (int i = len - 1; i >= 0; i--) {
            for (int j = len - 1; j >= i; j--) {
                if (ch[i] != ch[j]) {
                    arr[i][j] = false;
                } else {
                    if (i == j || i + 1 == j) {
                        arr[i][j] = true;
                    } else {
                        arr[i][j] = arr[i + 1][j - 1];
                    }
                }
            }
        }

        for (int i = 1; i < len; i++) {
            if (arr[0][i]) {
                dp[i] = 0;
            } else {
                for (int j = i; j > 0; j--) {
                    if (arr[j][i]) {
                        dp[i] = Math.min(dp[i], dp[j - 1] + 1);
                    }
                }
            }
        }

        System.out.println(Arrays.toString(dp));

        return dp[len - 1];
    }

    public static void main(String[] args) {
        System.out.println(minCut("aab"));
    }
}
